package ljye.com.ljye.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import ljye.com.ljye.R;
import ljye.com.ljye.adapter.ViewPagerAdapter;
import ljye.com.ljye.bean.Teacher_Home;
import ljye.com.ljye.dialog.ConsultDialog;
import ljye.com.ljye.fragment.Fragment_Teacher_home_evaluate;
import ljye.com.ljye.fragment.Fragment_Teacher_home_photo;
import ljye.com.ljye.fragment.Fragment_Teacher_home_resume;
import ljye.com.ljye.layout.NavitationLayout;
import ljye.com.ljye.utils.HttpUtils;
import ljye.com.ljye.utils.Utils;
import ljye.com.ljye.view.CustomPager;
import ljye.com.ljye.view.MyScrollView;
import okhttp3.Request;
import okhttp3.Response;

import static ljye.com.ljye.App.client;

public class Activity_Teacher_Home extends FragmentActivity implements MyScrollView.OnScrollListener {
    private View layoutHead;
    private ImageView imageView;
    private ImageView back;
    private ImageView share;
    private ImageView top;

    private NavitationLayout bar1;//title
    private ViewPagerAdapter viewPagerAdapter;//适配器
    private List<Fragment> fragments;//集合
    private CustomPager viewpager;

    private String[] titles = new String[]{"简历", "相册", "评论"};

    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.top://回顶
                    myScrollView.fullScroll(View.FOCUS_UP);
                    if (bar1.getParent()!=search02) {
                        search01.removeView(bar1);
                        search02.addView(bar1);
                    }
                    layoutHead.setBackgroundColor(Color.argb(0x0, 0x11, 0xb6, 0xf4));
                    top.setVisibility(View.GONE);
                    break;
                case R.id.consult://电话咨询
                    final ConsultDialog dialog=new ConsultDialog(Activity_Teacher_Home.this);
                    dialog.setMessage("4000-999-027");
                    dialog.setYesOnclickListener("呼叫", new ConsultDialog.onYesOnclickListener() {
                        @Override
                        public void onYesClick() {
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4000999027"));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            dialog.dismiss();

                        }
                    });
                    dialog.setNoOnclickListener("取消", new ConsultDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                    break;
                case R.id.appointment://预约试听

                    break;
                default:
                    break;
            }
        }
    };

    private MyScrollView myScrollView;
    private int searchLayoutTop;
    LinearLayout search01,search02;
    RelativeLayout rlayout;
    private int viewheight;
    private RelativeLayout viewHead;
    private LinearLayout mainLayout;
    private int mainheight;
    private LinearLayout consult;//电话咨询
    private LinearLayout appointment;//预约试听
    private String id;
    private Gson gson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_home);
        Utils.setColor(Activity_Teacher_Home.this, Color.parseColor("#FF8D59"));//橙色
        myScrollView = (MyScrollView)findViewById(R.id.myScrollView);
        search01 = (LinearLayout)findViewById(R.id.search01);
        search02 = (LinearLayout)findViewById(R.id.search02);
        rlayout = (RelativeLayout)findViewById(R.id.rlayout);
        //初始化控件
        initView();
        initPager();
        //接收上一个页面的数据
//        id = getIntent().getExtras().getString("teacher_id");
//        gson = new Gson();
//        if (null!= id){
//            getdata();
//        }
        back.setOnClickListener(listener);
        top.setOnClickListener(listener);
        consult.setOnClickListener(listener);
        appointment.setOnClickListener(listener);
        myScrollView.setOnScrollListener(this);

    }

    private void getdata() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Request request = new Request.Builder()
                            .url(HttpUtils.PATH + "/teacher/show/"+id)
                            .build();
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        Teacher_Home teacherhome = gson.fromJson(string, Teacher_Home.class);
                        final Teacher_Home.DataBean data = teacherhome.getData();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (null!=data){
                                    setdata();
                                }
                            }


                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void setdata() {

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            searchLayoutTop = mainheight-viewheight;//获取searchLayout的顶部位置
        }
    }

    private void initPager() {
        fragments = new ArrayList<>();
        fragments.add(new Fragment_Teacher_home_resume());
        fragments.add(new Fragment_Teacher_home_photo());
        fragments.add(new Fragment_Teacher_home_evaluate());
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(viewPagerAdapter);
//        0    --------   VISIBLE    可见
//        4    --------   INVISIBLE    不可见但是占用布局空间
//        8    --------   GONE    不可见也不占用布局空间
        //上下文        标题栏    ViewPager 未选中字体颜色         选中字体颜色          未选中字体大小 选中字体大小 当前viewpager的位置  导航条的边距 是否开启滑动
        bar1.setViewPager(Activity_Teacher_Home.this, titles, viewpager, R.color.text_gray, R.color.orange, 13, 13, 0, 30, true);
        bar1.setBgLine(Activity_Teacher_Home.this, 1, R.color.gray_background_color); //导航背景色
        bar1.setNavLine(Activity_Teacher_Home.this, 2, R.color.orange, 0);//导航条颜色

    }

    private void initView() {
        layoutHead = findViewById(R.id.view_head);
        imageView = (ImageView) findViewById(R.id.imageView1);
        back = (ImageView) findViewById(R.id.back);
        share = (ImageView) findViewById(R.id.share);

        consult = (LinearLayout) findViewById(R.id.consult);
        appointment = (LinearLayout) findViewById(R.id.appointment);
        viewHead = (RelativeLayout) findViewById(R.id.view_head);
        viewpager = (CustomPager) findViewById(R.id.viewpager2);
        mainLayout = (LinearLayout) findViewById(R.id.main_layout);
        bar1 = (NavitationLayout) findViewById(R.id.bar1);
        top = (ImageView) findViewById(R.id.top);
        layoutHead.setBackgroundColor(Color.argb(0, 0xff, 0x8d, 0x59));

        ViewTreeObserver viewTreeObserver = mainLayout.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mainheight = mainLayout.getHeight();
                mainLayout.getWidth();
            }
        });


        //head高度
        ViewTreeObserver viewvto = viewHead.getViewTreeObserver();
        viewvto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewHead.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                viewheight = viewHead.getHeight();
                viewHead.getWidth();
            }
        });

    }

    @Override
    public void onScroll(int y) {
        if(y >= searchLayoutTop){
            if (bar1.getParent()!=search01) {
                search02.removeView(bar1);
                search01.addView(bar1);
            }
        }else{
            if (bar1.getParent()!=search02) {
                search01.removeView(bar1);
                search02.addView(bar1);
            }
        }
        if(y<=searchLayoutTop){
            float scale = (float) y/(searchLayoutTop) ;

            float alpha =  scale<0?0:(255 * scale);
            Log.d("Activity_Teacher_AA", "scale:" + scale);
            Log.d("Activity_Teacher_AA", "alpha:" + alpha);
            //layout全部透明
//			layoutHead.setAlpha(scale);
            //只是layout背景透明(仿知乎滑动效果)
            layoutHead.setBackgroundColor(Color.argb((int)alpha, 0xff, 0x8d, 0x59));

        }
        else {
            layoutHead.setBackgroundColor(Color.parseColor("#FF8D59"));

        }
        if (y>searchLayoutTop){
            top.setVisibility(View.VISIBLE);
        }else {
            top.setVisibility(View.GONE);
        }
    }
}
